module.exports = ['$scope','$http', function UserController($scope,$http) {
    var _API = require('_API')($http);

    //获取全部用户列表
    function refresh() {
        _API.user.brief().success(function(res){
            $scope.data = res;
            $scope.users_user = {};
            $scope.isEdit = false;
        });
    }
    refresh();

    //每个用户的点击
    $scope.user_list = function (user,data) {
        //切换右边标题文字
        $scope.isEdit = true;
        $('.input-uid').prop('disabled',true);
        for(var i in data){
            data[i].isActive = false;
        };
        user.isActive = true;
        _API.user.get(user.uid).success(function(res){
            $scope.users_user = res;
            //不用angular bug太多
            if($scope.users_user.sex == 0){
                $('.sex-radio i').removeClass('active');
                $('#male').prop('checked',true).siblings('i').addClass('active');
            }else if($scope.users_user.sex == 1){
                $('.sex-radio i').removeClass('active');
                $('#female').prop('checked',true).siblings('i').addClass('active');
            }else{
                //不男不女
                $('.sex-radio i').removeClass('active');
                $('.sex-radio input').prop('checked',false);
            }
        });
    };

    // 删除用户
    $scope.user_del_submit = function (user,e) {
        e.stopPropagation();
        swal({
                title: "<small>确认删除用户 " + user.name + " 吗?</small>",
                html:true,
                showCancelButton: true,
                closeOnConfirm: false,
                showLoaderOnConfirm: true,
                confirmButtonText:"确定",
                cancelButtonText:"取消",
                confirmButtonColor:"#3c6",
                animation:false,
            },
            function(){
                _API.user.del(user.uid).success(function(){
                    swal.close();
                    refresh();
                    showToast('删除成功','ok');
            });
        });

    };


    // 选择性别
    $('.sex-radio').change(function(){
        $(this).find('i').addClass('active');
        $(this).siblings('.sex-radio').find('i').removeClass('active');
    });

    // 获取所有门店信息
    (function shop_all() {
        _API.shop.all().success(function(res){
            console.log(res);
            $scope.shop_all_data = res;
        });
    })();

    // 用户信息提交修改
    $scope.user_revise_submit = function () {
        var msg = {};
        if($scope.users_user.id) msg.id = $scope.users_user.id;
        if(!$scope.users_user.uid){
            showToast('账号不能为空','err');
            return;
        }else{
            msg.uid = $scope.users_user.uid;
        }
        if(!$scope.users_user.name){
            showToast('姓名不能为空','err');
            return;
        }else{
            msg.name = $scope.users_user.name;
        }
        if(!$scope.users_user.password){
            showToast('密码不能为空','err');
            return;
        }else{
            msg.password = $scope.users_user.password;
        }
        if(!$scope.users_user.shop_id){
            showToast('请选择门店','err');
            return;
        }else{
            msg.shop_id = $scope.users_user.shop_id;
        }
        if($scope.users_user.tel){
            if(checkTel($scope.users_user.tel)){
                msg.tel = $scope.users_user.tel;
            }else{
                return;
            }
        }
        if($scope.users_user.email){
            if(checkEmail($scope.users_user.email)){
                msg.email = $scope.users_user.email;
            }else{
                return;
            }
        }
        if($("input[name='sex']:checked").val()) msg.sex = Number($("input[name='sex']:checked").val());

        // console.log(msg);
        if($scope.isEdit){
            //修改
            _API.user.upd(msg).success(function(){
                refresh();
                showToast('修改用户成功','ok');
            });
        }else{
            // 新增
            _API.user.add(msg).success(function(){
                refresh();
                showToast('添加用户成功','ok');
            });
        }

    };

    // 增加用户
    $scope.addUser = function () {

        //切换右边标题文字
        $scope.isEdit = false;
        $('.input-uid').prop('disabled',false);
        //不选中用户
        for(var i in $scope.data){
            $scope.data[i].isActive = false;
        };
        console.log($('.input-uid'));
        $('.input-uid').focus();
        $scope.users_user = {};
        $('.sex-radio i').removeClass('active');
        $('.sex-radio input').prop('checked',false);


    };

    // 刷新用户列表
    $scope.syn_users_to_rbac = function() {
      _API.rbac.reload().success(function(){
          refresh();
          showToast('同步成功','ok');
      });
    }

}];